package day2.homework;

import javax.swing.text.html.InlineView;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

//统计一个文件testIO.txt  中字母'A'和'a'出现的总次数
public class Work4 {
    public static void main(String[] args) {
        fun1();
        fun2();
    }
    public static void fun1(){
        File f = new File("d://IO//testIO.txt");
        try (
             FileInputStream fis = new FileInputStream(f);
             BufferedInputStream bis = new BufferedInputStream(fis);
        ){
            int ACount = 0;
            int aCount = 0;
            for (int i = 0; i < f.length(); i++) {
                char n =(char)bis.read();
                if (n=='A') ACount++;
                else if (n=='a') aCount++;
            }
            System.out.println("A出现的次数:"+ACount);
            System.out.println("a出现的次数:"+aCount);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void fun2(){
        try (
                FileInputStream fis = new FileInputStream("d://IO//testIO.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
        ){
            byte[] bs = new byte[1];
            int len = 0;
            int ACount = 0;
            int aCount = 0;
            while ((len = bis.read(bs)) != -1){
                if (new String(bs).equals("A")){
                    ACount++;
                }else if (new String(bs).equals("a")){
                    aCount++;
                }
            }
            System.out.println("A出现的次数:"+ACount);
            System.out.println("a出现的次数:"+aCount);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
